
import VueRouter from "vue-router";
import Login from '../components/login'
import Home from '../components/home'
import Welcome from '../components/welcome'
import Users from '../components/user/users'
import Rights from '../components/power/rights'
import Roles from '../components/power/roles'
import Cate from '../components/goods/cate'
import Params from '../components/goods/params'
import Goodslist from '../components/goods/list'
import Add from '../components/goods/add'
import Order from '../components/order/order'
import Report from '../components/report/report'
const router = new VueRouter({
    routes:[
        {
            path:'/',
            redirect:'/login' //重定向
        },
        {
            path:'/login',
            component:Login
        },
        {
            path:'/home',
            component:Home,
            redirect:'/home/welcome',
            children:[
                {
                    name:'welcome',
                    path:'/home/welcome',
                    component:Welcome
                },
                {
                    name:'users',
                    path:'/home/users',
                    component:Users
                },
                {
                    name:'rights',
                    path:'/home/rights',
                    component:Rights
                },
                {
                    name:'roles',
                    path:'/home/roles',
                    component:Roles
                },
                {
                    name:'categories',
                    path:'/home/categories',
                    component:Cate
                },
                {
                    name:'params',
                    path:'/home/params',
                    component:Params
                },
                {
                    name:'list',
                    path:'/home/goods',
                    component:Goodslist,
                },
                {
                    name:'add',
                    path:'/home/add',
                    component:Add
                },
                {
                    name:'order',
                    path:'/home/orders',
                    component:Order
                },
                {
                    name:'report',
                    path:'/home/reports',
                    component:Report
                }
            ]
        }
    ]
})
// 挂在路由守卫
router.beforeEach((to,form,next)=>{
    if(to.path === '/login' ) return next();
    // 获取token
    const tokenstr = window.sessionStorage.getItem('token')
    // 如果没有token就直接跳转到登录页面
    if(!tokenstr) return next('/login')
    next();
})
export default router
